﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;
using VSLangProj;

namespace DaveSexton.Labs.Wizards
{
	[CLSCompliant(false)]
	public sealed class LabItemTemplateWizard : IWizard
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		public LabItemTemplateWizard()
		{
		}
		#endregion

		#region Methods
		private static void SetProjectRootNamespaceToken(object automationObject, Dictionary<string, string> replacementsDictionary)
		{
			Contract.Requires(replacementsDictionary != null);

			var dte = (DTE) automationObject;

			Contract.Assume(dte != null);

			var projects = (object[]) dte.ActiveSolutionProjects;

			Contract.Assume(projects != null);
			Contract.Assume(projects.Length > 0);

			var project = (Project) projects[0];

			Contract.Assume(project != null);

			var langProject = project.Object as VSProject;

			if (langProject != null)
			{
				var properties = project.Properties;

				Contract.Assume(properties != null);

				var rootNamespace = properties.Item("RootNamespace");

				Contract.Assume(rootNamespace != null);

				replacementsDictionary["$projectrootnamespace$"] = (string) rootNamespace.Value;
			}
		}
		#endregion

		#region IWizard Members
		public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
		{
			Contract.Assume(replacementsDictionary != null);

			SetProjectRootNamespaceToken(automationObject, replacementsDictionary);
		}

		public bool ShouldAddProjectItem(string filePath)
		{
			return true;
		}

		public void ProjectItemFinishedGenerating(ProjectItem projectItem)
		{
		}

		public void RunFinished()
		{
		}

		public void BeforeOpeningFile(ProjectItem projectItem)
		{
		}

		public void ProjectFinishedGenerating(Project project)
		{
		}
		#endregion
	}
}